基于OpenResty部署应用层nginx以及nginx+lua开发hello world

发布 : 2017-07-05 分类 : 大数据 浏览 :

部署第一个nginx,作为应用层nginx[192.168.31.231]

部署openresty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@matrix-cache01 ~]# mkdir -p /usr/servers
[root@matrix-cache01 ~]# cd /usr/servers/

[root@matrix-cache01 servers]# yum install -y readline-devel pcre-devel openssl-devel gcc

[root@matrix-cache01 servers]# wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz
[root@matrix-cache01 servers]# tar -xzvf ngx_openresty-1.7.7.2.tar.gz
[root@matrix-cache01 servers]# cd /usr/servers/ngx_openresty-1.7.7.2/

[root@matrix-cache01 ngx_openresty-1.7.7.2]# cd bundle/LuaJIT-2.1-20150120/
[root@matrix-cache01 LuaJIT-2.1-20150120]# make clean && make && make install
[root@matrix-cache01 LuaJIT-2.1-20150120]# ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit

[root@matrix-cache01 ~]# cd /usr/servers/ngx_openresty-1.7.7.2/
[root@matrix-cache01 ngx_openresty-1.7.7.2]# cd bundle
[root@matrix-cache01 bundle]# wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
[root@matrix-cache01 bundle]# tar -xvf 2.3.tar.gz

[root@matrix-cache01 ~]# cd /usr/servers/ngx_openresty-1.7.7.2/
[root@matrix-cache01 ngx_openresty-1.7.7.2]# cd bundle
[root@matrix-cache01 bundle]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
[root@matrix-cache01 bundle]# tar -xvf v0.3.0.tar.gz

[root@matrix-cache01 bundle]# cd /usr/servers/ngx_openresty-1.7.7.2
[root@matrix-cache01 ngx_openresty-1.7.7.2]# ./configure --prefix=/usr/servers --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2
[root@matrix-cache01 ngx_openresty-1.7.7.2]# make && make install

[root@matrix-cache01 ngx_openresty-1.7.7.2]# cd /usr/servers/
[root@matrix-cache01 servers]# ll
总用量 20
drwxr-xr-x 2 root root 4096 7月 6 07:34 bin
drwxr-xr-x 6 root root 4096 7月 6 07:34 luajit
drwxr-xr-x 5 root root 4096 7月 6 07:34 lualib
drwxr-xr-x 6 root root 4096 7月 6 07:34 nginx
drwxrwxr-x 4 1000 1000 4096 7月 6 07:33 ngx_openresty-1.7.7.2

查看安装的Nginx版本

1
[root@matrix-cache01 servers]# nginx/sbin/nginx -V

nginx+lua开发的hello world

1
[root@matrix-cache01 ~]# vi /usr/servers/nginx/conf/nginx.conf

在http部分添加

1
2
lua_package_path "/usr/servers/lualib/?.lua;;";  
lua_package_cpath "/usr/servers/lualib/?.so;;";

/usr/servers/nginx/conf下,创建一个lua.conf

1
2
[root@matrix-cache01 ~]# cd /usr/servers/nginx/conf
[root@matrix-cache01 conf]# vi lua.conf
1
2
3
4
server {
listen 80;
server_name _;
}

在nginx.conf的http部分添加:

1
2
3
4
[root@matrix-cache01 ~]# vi /usr/servers/nginx/conf/nginx.conf
http{
include lua.conf;
}

验证配置是否正确

1
[root@matrix-cache01 ~]# /usr/servers/nginx/sbin/nginx -t

在lua.conf的server部分添加

1
2
3
4
5
[root@matrix-cache01 ~]# vi /usr/servers/nginx/conf/lua.conf
location /lua {
default_type 'text/html';
content_by_lua 'ngx.say("hello world")';
}

验证配置是否正确

1
[root@matrix-cache01 ~]# /usr/servers/nginx/sbin/nginx -t

重新nginx加载配置

1
2
3
4
5
6
7
8
9
10
11
12
[root@matrix-cache01 ~]# /usr/servers/nginx/sbin/nginx -s reload
```

### 访问http://192.168.31.231/lua

![](http://ww1.sinaimg.cn/large/dc05ba18gy1fhaynr45bkj20yw086jt2.jpg)

### 修改hello.lua

```bash
[root@matrix-cache01 ~]# vi /usr/servers/nginx/conf/lua/hello.lua
ngx.say("hello world! this is matrix-cache01!")

修改lua.conf

1
2
3
4
5
[root@matrix-cache01 ~]# vi /usr/servers/nginx/conf/lua.conf
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/hello.lua;
}

访问http://192.168.31.231/lua

查看异常日志

1
[root@matrix-cache01 ~]# tail -f /usr/servers/nginx/logs/error.log

工程化的nginx+lua项目结构

项目工程结构

1
2
3
4
5
6
7
hello
hello.conf
lua
hello.lua
lualib
*.lua
*.so

配置/usr/hello/lua/hello.lua

1
2
3
[root@matrix-cache01 ~]# mkdir -p /usr/hello/lua/
[root@matrix-cache01 ~]# vi /usr/hello/lua/hello.lua
ngx.say("hello world! this is matrix-cache01!")

配置/usr/hello/hello.conf

1
2
3
4
5
6
7
8
9
10
[root@matrix-cache01 ~]# vi /usr/hello/hello.conf
server {
listen 80;
server_name _;

location /hello {
default_type 'text/html';
content_by_lua_file /usr/hello/lua/hello.lua;
}
}

编辑nginx配置文件

1
2
3
4
5
6
[root@matrix-cache01 ~]# vi /usr/servers/nginx/conf/nginx.conf
http {
lua_package_path "/usr/hello/lualib/?.lua;;";
lua_package_cpath "/usr/hello/lualib/?.so;;";
include /usr/hello/hello.conf;
}

拷贝lualib

1
2
3
4
5
6
[root@matrix-cache01 ~]# cp -r /usr/servers/lualib /usr/hello/
[root@matrix-cache01 hello]# ll
总用量 12
-rw-r--r-- 1 root root 175 7月 7 08:03 hello.conf
drwxr-xr-x 2 root root 4096 7月 7 07:40 lua
drwxr-xr-x 5 root root 4096 7月 7 07:55 lualib

验证配置是否正确

1
[root@matrix-cache01 ~]# /usr/servers/nginx/sbin/nginx -t

重新nginx加载配置

1
[root@matrix-cache01 ~]# /usr/servers/nginx/sbin/nginx -s reload

访问http://192.168.31.231/hello

基于OpenResty在另外两台机器上都部署一下nginx+lua的开发环境


本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2017/07/05/基于OpenResty部署应用层nginx以及nginx+lua开发hello world/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹